from sqlalchemy import Column, Integer, String, Text, Boolean, ForeignKey, DateTime, Float
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.db.database import Base


class Merchant(Base):
    """商家模型"""
    __tablename__ = "merchants"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, index=True)
    logo = Column(String(255), nullable=True)
    description = Column(Text, nullable=True)
    address = Column(String(255), nullable=True)
    phone = Column(String(20), nullable=True)
    email = Column(String(100), nullable=True)
    business_license = Column(String(255), nullable=True)  # 营业执照图片
    latitude = Column(Float, nullable=True)
    longitude = Column(Float, nullable=True)
    
    # 商家状态
    is_verified = Column(Boolean, default=False)  # 是否已认证
    is_active = Column(Boolean, default=True)  # 是否激活
    is_deleted = Column(Boolean, default=False)  # 是否被删除（软删除）
    
    # 关联用户（一个商家对应一个用户账号）
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False, unique=True)
    user = relationship("User", back_populates="merchant")
    
    # 关联营地（一个商家可以有多个营地）
    campsites = relationship("Campsite", back_populates="merchant")
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    def __repr__(self):
        return f"<Merchant {self.name}>"


class MerchantImage(Base):
    """商家图片模型"""
    __tablename__ = "merchant_images"
    
    id = Column(Integer, primary_key=True, index=True)
    merchant_id = Column(Integer, ForeignKey("merchants.id"), nullable=False)
    url = Column(String(255), nullable=False)
    description = Column(String(255), nullable=True)
    is_cover = Column(Boolean, default=False)  # 是否为封面图
    
    # 关联商家
    merchant = relationship("Merchant", backref="images")
    
    # 时间戳
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now()) 